package pw.archipelag.templateGame {
	import flash.display.Sprite;
	import pw.archipelag.loaderGame.interfaceGame.IGame;
	import flash.display.DisplayObjectContainer;
	import pw.archipelag.loaderGame.interfaceGame.DescriptionPlayer;
	import flash.text.TextField;
	import flash.text.Font;
	import base.tf.TextFieldUtils;
	import flash.events.Event;
	import flash.utils.setTimeout;
	
	import pw.archipelag.loaderGame.interfaceGame.skinsPanel.SkinPanelSet;
	import flash.display.Shape;
	import pw.archipelag.loaderGame.interfaceGame.skinsPanel.SkinPanelMini;
	import flash.geom.Rectangle;
	import flash.text.TextFormat;
	import flash.geom.Point;
	import pw.archipelag.loaderGame.interfaceGame.skinsPanel.SkinBg;
	import pw.archipelag.loaderGame.interfaceGame.skinsPanel.SkinTf;
	import pw.archipelag.loaderGame.interfaceGame.skinsPanel.SkinBtnPanel;
	import pw.archipelag.loaderGame.interfaceGame.skinsPanel.SkinScores;
	import pw.archipelag.loaderGame.interfaceGame.skinsPanel.SkinHelp;
	
	import pw.archipelag.loaderGame.interfaceGame.EventLoaderGame;
	import flash.utils.clearTimeout;
	
	public class TemplateGame extends Sprite implements IGame {
		
		[Embed(source = "../../../../mat/font/cour.ttf", fontName="Courier New", mimeType = 'application/x-font', embedAsCFF='false', unicodeRange='U+0020-U+007E, U+0105, U+0119, U+00F3, U+0142, U+017C, U+017a, U+0107, U+015B, U+0144, U+0104, U+0118, U+00D3, U+0141, U+017B, U+0179, U+0106, U+015A, U+0143, U+00A0, U+F0B7')]
		public static const classFontCourier: Class;
		
		[Embed(source = "../../../../mat/font/ANTQUABI.TTF", fontName="Book Antiqua", mimeType = 'application/x-font', embedAsCFF='false', unicodeRange='U+0020-U+007E, U+0105, U+0119, U+00F3, U+0142, U+017C, U+017a, U+0107, U+015B, U+0144, U+0104, U+0118, U+00D3, U+0141, U+017B, U+0179, U+0106, U+015A, U+0143, U+00A0, U+F0B7')]
		public static const classFontBookAntiquaBI: Class;
		
		[Embed(source="!embed/assetsTemplateGame.swf", symbol="mcHelp")]
		public static const classMcHelp: Class;
		
		private var descriptionPlayer: DescriptionPlayer;
		private var containerPanels: DisplayObjectContainer;
		private var tfLabel: TextField;
		private var btnStage: Sprite;
		private var timeoutEndGame: uint;
		
		public function TemplateGame(): void {
			this.initFonts();
			//tu kod inicjujący grę
			this.createTfLabel();
		}
		
		private function initFonts(): void {
			Font.registerFont(TemplateGame.classFontCourier);
			Font.registerFont(TemplateGame.classFontBookAntiquaBI);
			TextFieldUtils.traceFonts();
		}
		
		private function createTfLabel(): void {
			this.tfLabel = TextFieldUtils.createTextField();
			this.tfLabel.text = this.nameGame;
			this.addChild(this.tfLabel)
		}
		
		public function init(descriptionPlayer: DescriptionPlayer) {
			this.descriptionPlayer = descriptionPlayer;
			////tu kod inicjujący grę (tu jest dostępny this.stage)
			this.tfLabel.appendText("\ninicjacja gry\ngracz:" + this.descriptionPlayer);
			this.createBtnStage();
			this.readAssets();
			return true;
		}
		
		private function createBtnStage(): void {
			this.btnStage = new Sprite();
			this.btnStage.graphics.beginFill(0xFFFFFF, 0);
			this.btnStage.graphics.drawRect(0, 0, 1, 1);
			this.btnStage.graphics.endFill();
			this.btnStage.buttonMode = true;
			this.addChildAt(this.btnStage, 0);
			this.stage.addEventListener(Event.RESIZE, this.setDimensionBtnStageOnStageResize)
			this.stage.dispatchEvent(new Event(Event.RESIZE));
		}
		
		private function setDimensionBtnStageOnStageResize(e: Event): void {
			this.btnStage.width = this.stage.stageWidth;
			this.btnStage.height = this.stage.stageHeight;
		}
		
		private function readAssets():void {
			this.tfLabel.appendText("\nwczytywanie danych w grze");
			setTimeout(this.finishReadAssets, 3000);
		}
		
		private function finishReadAssets():void {
			this.tfLabel.appendText("\nkoniec wczytywanie danych w grze");
			this.dispatchEvent(new EventLoaderGame(EventLoaderGame.RESTART_GAME));	
		}
		
		
		//oprogramowanie interfejsu
		
		public function get nameGame(): String {
			return "Gra \"Szablon\"";
		}
		
		public function get classMcHelp(): Class {
			//albo dbamy o to że tło (bounding box) dla całego obszaru mc pomocy było narysowane, albo podajemy rozmiar całego mc pomocy w parametrze do SkinHelp
			return TemplateGame.classMcHelp;
		}
		
		public function get skinPanelSet(): SkinPanelSet {
			var shapeBgBtnPanel: Shape = new Shape();
			shapeBgBtnPanel.graphics.beginFill(0x006600, 1);
			shapeBgBtnPanel.graphics.moveTo(0, 20);
			shapeBgBtnPanel.graphics.lineTo(80, 0);
			shapeBgBtnPanel.graphics.lineTo(100, 80);
			shapeBgBtnPanel.graphics.lineTo(20, 100);
			shapeBgBtnPanel.graphics.lineTo(0, 20);
			shapeBgBtnPanel.graphics.endFill();
			shapeBgBtnPanel.graphics.beginFill(0x003300, 1);
			shapeBgBtnPanel.graphics.drawRect(10, 10, 80, 80);
			shapeBgBtnPanel.graphics.endFill();
			var radiusForScaling: Number = 20;
			var skinPanelMini: SkinPanelMini = new SkinPanelMini(new Rectangle(10, 10, 130, 250), 2, 1);
			var dimension: Rectangle = new Rectangle(0, 0, 700, 600);
			var skinBg: SkinBg = new SkinBg(shapeBgBtnPanel, 0.7, radiusForScaling, { bevelFilter: { blurX:5, blurY:5, strength:1, angle:45, distance:5, shadowColor: 0x001100, highlightColor: 0x005500 }} );
			var skinTfLabelTitles: SkinTf = new SkinTf(new TextFormat("Book Antiqua", 25, 0x339900), {dropShadowFilter: {color:0x999900, alpha:1, strength: 1, blurX:0, blurY:0, angle:45, distance: 2}})
			var skinBtnPanel: SkinBtnPanel = new SkinBtnPanel(new SkinBg(shapeBgBtnPanel, 1, radiusForScaling), new SkinTf(new TextFormat("Book Antiqua", 20, 0xFF9900)), new Point(15, 15));
			var skinScores: SkinScores = new SkinScores(new SkinTf(new TextFormat("Courier New", 20, 0x333300), {glowFilter: {color:0x666600, alpha:1, blurX:2, blurY:2, strength:3, quality: 2}}), 0xFFFF00, new SkinTf(new TextFormat("Courier New", 18, 0x009900), {dropShadowFilter: {color:0x999900, alpha:1, strength: 1, blurX:0, blurY:0, angle:45, distance: 2}}), 0xAA996600, 0xAAFFCC00, 0x006600, 0x009900, 20, 50);
			var skinHelp: SkinHelp = new SkinHelp(new Rectangle(0, 0, 504, 508));
			return new SkinPanelSet(skinPanelMini, dimension, skinBg, skinTfLabelTitles, skinBtnPanel, skinScores, skinHelp);
		}
		
		public function start(): void {
			this.tfLabel.appendText("\nstart gry");
			//tu kod rozpoczynający grę
			this.timeoutEndGame = setTimeout(this.end, 3000);
		}
		
		public function set isPauseContinue(value: uint): void {
			this.tfLabel.appendText("\n" + ["pauza gry", "kontynuacja gry"][value]);
			if (value == 0) clearTimeout(this.timeoutEndGame);
			else if (value == 1) this.timeoutEndGame = setTimeout(this.end, 3000);
		}
		
		//koniec gry
		
		private function end(): void {
			this.tfLabel.appendText("\nkoniec gry i zapisanie wyniku");
			var scorePoints: uint = Math.floor(Math.random() * 1000);
			var scorePercent: uint = Math.round(scorePoints / 11);
			this.dispatchEvent(new EventLoaderGame(EventLoaderGame.SAVE_SCORE, {scorePoints: scorePoints, scorePercent: scorePercent}));
		}
		
	}
	
}